package org.jeecg.modules.bobo.wechat.miniapp.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.bobo.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: jeecg-boot-parent
 * @description: 小程序公共接口
 * @author: 三千霜
 * @create: 2020-04-23 10:49
 */
@RestController
@RequestMapping("/api/wx/miniapp/user")
@Api(tags = "微信模块小程序api")
@Slf4j
@ApiSort(value = 3)
public class WxMaUserApi {
    @Autowired
    private WxMaService wxMaService;
    /**
     * 登陆接口
     */
    @GetMapping("/login")
    @ApiOperation("登陆接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "code")
    })
    public Result<WxMaJscode2SessionResult> login(@RequestParam(name="code") String code) {
        if (StringUtils.isBlank(code)) {
            return  Result.error("empty jscode");
        }
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
            //TODO 可以增加自己的逻辑，关联业务相关数据
            return Result.success(session);
        } catch (WxErrorException e) {
            return Result.error(e.toString());
        }
    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @GetMapping("/info")
    @ApiOperation("获取用户信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionKey",value = "sessionKey"),
            @ApiImplicitParam(name = "signature",value = "signature"),
            @ApiImplicitParam(name = "rawData",value = "rawData"),
            @ApiImplicitParam(name = "encryptedData",value = "encryptedData"),
            @ApiImplicitParam(name = "iv",value = "iv"),
    })
    public Result<WxMaUserInfo> info(@RequestParam(name="sessionKey") String sessionKey, @RequestParam(name="signature") String signature,@RequestParam(name="rawData") String rawData,@RequestParam(name="encryptedData") String encryptedData,@RequestParam(name="iv") String iv) {




        // 用户信息校验
        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return Result.error("user check failed");
        }

        // 解密用户信息
        WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);

        return Result.success(userInfo);
    }

    /**
     * <pre>
     * 获取用户绑定手机号信息
     * </pre>
     */
    @GetMapping("/phone")
    @ApiOperation("获取用户绑定手机号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionKey",value = "sessionKey"),
            @ApiImplicitParam(name = "signature",value = "signature"),
            @ApiImplicitParam(name = "rawData",value = "rawData"),
            @ApiImplicitParam(name = "encryptedData",value = "encryptedData"),
            @ApiImplicitParam(name = "iv",value = "iv"),
    })
    public Result<WxMaPhoneNumberInfo> phone(@RequestParam(name="sessionKey") String sessionKey, @RequestParam(name="signature") String signature,@RequestParam(name="rawData") String rawData,@RequestParam(name="encryptedData") String encryptedData,@RequestParam(name="iv") String iv) {

        // 用户信息校验
        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return Result.error("user check failed");
        }

        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);

        return Result.success(phoneNoInfo);
    }
}
